package org.dromara.quick.chat.service.impl;

import cn.dev33.satoken.secure.BCrypt;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.IdGenerator;
import org.dromara.common.redis.utils.RedisUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.constant.CacheConstants;
import org.dromara.quick.chat.domain.ImSocialPrivacySetting;
import org.dromara.quick.chat.mapper.ImSocialPrivacySettingMapper;
import org.dromara.quick.chat.model.bo.query.ImSocialPrivacySettingBO;
import org.dromara.quick.chat.model.dto.ChatRecordPasswordDTO;
import org.dromara.quick.chat.model.dto.IntegralPasswordDTO;
import org.dromara.quick.chat.model.vo.ImSocialPrivacySettingVO;
import org.dromara.quick.chat.service.ImSocialPrivacySettingService;
import org.dromara.resource.api.RemoteSmsService;
import org.dromara.system.api.RemoteUserService;
import org.springframework.stereotype.Service;

import java.time.Duration;

/**
* @author Stars
* @description 针对表【im_social_privacy_setting(社交软件中的隐私设置表，用于存储用户的隐私设置信息)】的数据库操作Service实现
* @createDate 2025-03-04 22:33:21
*/
@RequiredArgsConstructor
@Service
public class ImSocialPrivacySettingServiceImpl extends ServiceImpl<ImSocialPrivacySettingMapper, ImSocialPrivacySetting>
    implements ImSocialPrivacySettingService{

    private final ImSocialPrivacySettingMapper imSocialPrivacySettingMapper;

    @DubboReference
    private final RemoteSmsService remoteSmsService;

    @DubboReference
    private final RemoteUserService remoteUserService;

    @Override
    public ImSocialPrivacySettingVO selectPrivacySetting(Long userId) {
        ImSocialPrivacySettingVO cacheObject = RedisUtils.getCacheObject(CacheConstants.IM_SOCIAL_PRIVACY_SETTING + userId);
        if (cacheObject != null) {
            return cacheObject;
        }

        ImSocialPrivacySettingVO imSocialPrivacySettingVO = imSocialPrivacySettingMapper.selectVoOne(new LambdaQueryWrapper<ImSocialPrivacySetting>().eq(ImSocialPrivacySetting::getUserId, userId));
        if (imSocialPrivacySettingVO == null) {
            return imSocialPrivacySettingVO;
        }
        if (StringUtils.isNoneEmpty(imSocialPrivacySettingVO.getChatRecordPassword())) {
            imSocialPrivacySettingVO.setChatRecordPassword("******");
        }
        if (StringUtils.isNotEmpty(imSocialPrivacySettingVO.getCreditPaymentPassword())) {
            imSocialPrivacySettingVO.setCreditPaymentPassword("******");
        }
        RedisUtils.setCacheObject(CacheConstants.IM_SOCIAL_PRIVACY_SETTING + userId, imSocialPrivacySettingVO, Duration.ofDays(1));
        return imSocialPrivacySettingVO;
    }

    @Override
    public void updatePrivacySetting(ImSocialPrivacySettingBO imSocialPrivacySettingBO) {
        Long userId = LoginHelper.getUserId();
        ImSocialPrivacySetting imSocialPrivacySetting = new ImSocialPrivacySetting();
        BeanUtil.copyProperties(imSocialPrivacySettingBO,imSocialPrivacySetting);
        LambdaQueryWrapper<ImSocialPrivacySetting> lambdaQueryWrapper = new LambdaQueryWrapper<ImSocialPrivacySetting>().eq(ImSocialPrivacySetting::getUserId, userId);
        // 查询是否存在
        if (imSocialPrivacySettingMapper.exists(lambdaQueryWrapper)){
            // 存在
            imSocialPrivacySettingMapper.update(imSocialPrivacySetting,lambdaQueryWrapper);
            return;
        }
        // 添加隐私设置
        imSocialPrivacySetting.setId(IdGenerator.getId());
        imSocialPrivacySetting.setUserId(userId);
        imSocialPrivacySettingMapper.insert(imSocialPrivacySetting);
    }

    @Override
    public void updateChatRecordPassword(ChatRecordPasswordDTO chatRecordPasswordDTO) {
        Long userId = LoginHelper.getUserId();
        // 查询手机号
        String phone = remoteUserService.selectPhonenumberById(userId);

        // 短信的验证码时ruoyi-resource服务发送的，以防以后俩几个微服务连接的redis不同，所以也让发送验证码的服务去验证
        boolean validateSmsCode = remoteSmsService.validateSmsCode(phone, chatRecordPasswordDTO.getCode());
        if (!validateSmsCode) {
            throw new ServiceException("验证码错误");
        }
        LambdaQueryWrapper<ImSocialPrivacySetting> lambdaQueryWrapper = new LambdaQueryWrapper<ImSocialPrivacySetting>().eq(ImSocialPrivacySetting::getUserId, userId);
        ImSocialPrivacySetting imSocialPrivacySetting = new ImSocialPrivacySetting();
        imSocialPrivacySetting.setChatRecordPassword(BCrypt.hashpw(chatRecordPasswordDTO.getPassword()));
        // 判断是否存在隐私设置
        if (imSocialPrivacySettingMapper.exists(lambdaQueryWrapper)){
            // 存在
            imSocialPrivacySettingMapper.update(imSocialPrivacySetting,lambdaQueryWrapper);
            return;
        }
        // 添加隐私设置
        imSocialPrivacySetting.setId(IdGenerator.getId());
        imSocialPrivacySetting.setUserId(userId);
        imSocialPrivacySettingMapper.insert(imSocialPrivacySetting);
    }

    @Override
    public void updateIntegralPassword(IntegralPasswordDTO integralPasswordDTO) {
        Long userId = LoginHelper.getUserId();
        // 查询手机号
        String phone = remoteUserService.selectPhonenumberById(userId);

        // 短信的验证码时ruoyi-resource服务发送的，以防以后俩几个微服务连接的redis不同，所以也让发送验证码的服务去验证
        boolean validateSmsCode = remoteSmsService.validateSmsCode(phone, integralPasswordDTO.getCode());
        if (!validateSmsCode) {
            throw new ServiceException("验证码错误");
        }
        LambdaQueryWrapper<ImSocialPrivacySetting> lambdaQueryWrapper = new LambdaQueryWrapper<ImSocialPrivacySetting>().eq(ImSocialPrivacySetting::getUserId, userId);
        ImSocialPrivacySetting imSocialPrivacySetting = new ImSocialPrivacySetting();
        imSocialPrivacySetting.setCreditPaymentPassword(BCrypt.hashpw(integralPasswordDTO.getPassword()));
        // 判断是否存在隐私设置
        if (imSocialPrivacySettingMapper.exists(lambdaQueryWrapper)){
            // 存在
            imSocialPrivacySettingMapper.update(imSocialPrivacySetting,lambdaQueryWrapper);
            return;
        }
        // 添加隐私设置
        imSocialPrivacySetting.setId(IdGenerator.getId());
        imSocialPrivacySetting.setUserId(userId);
        imSocialPrivacySettingMapper.insert(imSocialPrivacySetting);

    }
}




